

from wsgiref import simple_server
from pyamf.remoting.gateway.wsgi import WSGIGateway

class FlexService:
    def echo(self, data):
        print "I am in Echo"
        return data; 
    """
    def stopGUIServer(self):
        print "I am in stopGUIServer"
        httpd.server_close()
        print "I am exiting"
        sys.exit()
    
    def stopTask(self, id):
        print "I am in stopTask in flex_gui"
        frontend.stop_task(id)
        return id
    
    def deletePopulation(self, schema_name, pop_name):
        frontend.deletePopulation(schema_name, pop_name)
    
    def getTaskState(self, id):
        try:
            return frontend.getTaskState(id)
        except:
            print "Exception", sys.exc_info()
    
    def getTaskStates(self):
        return frontend.getTaskStates()
      
    def connectToServer(self, url):
        #print "The url is", url
        self.url = url
        frontend.connect_to_server(url)
    
    def createNewSchema(self, schema_name, folder_name):
        #print "I am in createNewSchema", schema_name, folder_name
        try:
            frontend.create_new_schema(schema_name, folder_name)
        except:
            print "Exception", sys.exc_info()
    
    def runConfigFunction(self, schema_name, config_func_name):
        try:
            #print schema_name, config_func_name
            pop_name = frontend.assign_pop_name(schema_name, func_name=config_func_name)
            print schema_name, pop_name, config_func_name
            frontend.run_config_function(schema_name, pop_name, config_func_name)
        except:
            print "Exception", sys.exc_info()
        return pop_name
    
    def runBatchTasks(self, schema_name, pop_name, func_name):
        try:
            return frontend.run_batch_tasks(schema_name, pop_name, func_name)
        except:
            print "Exception", sys.exc_info()
            print traceback.print_tb(sys.exc_traceback)
    
    def runTask(self, schema_name, pop_name, script_name, run_type):
        return frontend.run_task(schema_name, pop_name, script_name, run_type)

    def getSchemaNames(self):
        return frontend.get_schema_names()
    
    def getLocalSchemaFolderNames(self):
        return frontend.getLocalSchemaFolderNames()
    
    def getConfigFunctionNames(self, schema_name):
        print "I am in getConfigFunctionNames"
        x = frontend.getConfigFunctionNames(schema_name)
        print x
        return x
    
    def getPopulationNames(self, schema_name):
        return frontend.get_population_names(schema_name)
    
    def getAutoStartFunctionNames(self, schema_name):
        return frontend.getAutoStartFunctionNames(schema_name)
    
    def getScriptNames(self, schema_name, pop_name):
        return frontend.getScriptNames(schema_name, pop_name)
    
    def getRunTypes(self):
        return [constants.ONCE, constants.LOOPED]
    """
    
services = {
    'pyamfService':FlexService,
}

if __name__ == "__main__":
    gw = WSGIGateway(services)
    
    httpd = simple_server.WSGIServer( ("172.20.34.116", 8000),
                                      simple_server.WSGIRequestHandler)  
    httpd.set_app(gw)
    print "Running Hello World AMF gateway on http://localhost:8000"
        
    try:
        httpd.serve_forever()
    except:
        print "I am in except haha "

